package cn.edu.nuc.onlinestore.frame;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.edu.nuc.onlinestore.network.server.TCPClient;
import cn.edu.nuc.onlinestore.vo.AdminDo;
import cn.edu.nuc.onlinestore.vo.Massage;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.ImageIcon;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AdminLogin extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5976800248183993803L;
	private JPanel contentPane;
	private JTextField adminname;
	private JPasswordField adminpassword;
	private JFrame jframe;
	private static TCPClient tcp=null;
	private String result;
	private String massage;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//ChooseServer fram=new ChooseServer();
					//tcp=new TCPClient(fram.getIp(),fram.getDk());
					//AdminLogin frame = new AdminLogin();
					//frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @param dk 
	 * @param ip2 
	 */
	public AdminLogin(String ip, int dk) {
		tcp=new TCPClient(ip,dk);
		this.jframe=this;
		setTitle("中北线在商场管理系统-管理员登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 461, 349);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("用户名:");
		label.setBounds(87, 146, 54, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("密  码:");
		label_1.setBounds(87, 185, 54, 15);
		contentPane.add(label_1);
		
		adminname = new JTextField();
		adminname.setText("kangli");
		adminname.setBounds(151, 146, 197, 21);
		contentPane.add(adminname);
		adminname.setColumns(10);
		
		adminpassword = new JPasswordField();
		adminpassword.setBounds(151, 185, 197, 21);
		contentPane.add(adminpassword);
		
		JButton button = new JButton("登录系统");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//判断登录成功与否
				StringBuilder adminpass=new StringBuilder();
				String adminpasswor=adminpass.append(adminpassword.getPassword()).toString();
				//System.out.println(adminname.getText()+adminpass.append(adminpassword.getPassword()));//测试
				if(adminname.getText().equals("")||adminpasswor.equals("")){
					if(adminname.getText().equals("")){
						JOptionPane.showMessageDialog(null, "用户名不能为空");
					}else if(adminpasswor.equals("")){
						JOptionPane.showMessageDialog(null, "密码不能为空");
						}
				}else{
						//System.out.println(adminpasswor+"--");
						AdminDo adminlogin=new AdminDo(adminname.getText(),adminpasswor);
						Massage msg=new Massage(0,"","check",adminlogin);
						tcp.sendOptmessage(msg);
						result=tcp.getResult();
						massage=tcp.getmsg();
						if(result.equals("true")){
							JOptionPane.showMessageDialog(null,massage);
							AdminStore frame = new AdminStore(jframe,adminname.getText(),tcp);
							frame.setVisible(true);
							jframe.setVisible(false);
						}else{
							JOptionPane.showMessageDialog(null,massage);
							adminname.setText(null);
							adminpassword.setText(null);
						}
				}
			}
		});
		button.setBounds(255, 216, 93, 23);
		contentPane.add(button);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(AdminLogin.class.getResource("../images/a.jpg")));
		lblNewLabel.setBounds(208, 54, 86, 82);
		contentPane.add(lblNewLabel);
	}
}
